package com.example.aqarapp.view;

import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.CountDownTimer;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProviders;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.aqarapp.viewmodel.LoginViewModel;

import aqarapp.R;
import aqarapp.databinding.AparaLoginBinding;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = "BULB_CONTROL_DIALOG";
    Button btn;

    private ImageView sendImage;
    private TextView sendText;

    private EditText userName;
    private EditText password;

    AparaLoginBinding binding;
    LoginViewModel loginVM;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.apara_login);
//        init();
        binding = DataBindingUtil.setContentView(this, R.layout.apara_login);
        loginVM = ViewModelProviders.of(this).get(LoginViewModel.class);
        binding.setVm(loginVM);
        binding.setLifecycleOwner(this);
        init();
    }

    private void init(){
        userName = (EditText) findViewById(R.id.et_userName);
        password = (EditText) findViewById(R.id.et_password);
        btn = (Button) findViewById(R.id.btn_varify);

        sendText = (TextView) findViewById(R.id.notetext);
        sendImage = (ImageView) findViewById(R.id.send_image);

        EditTextClearTools.addClearListener(userName, btn);

        final MyCountDownTimer myCountDownTimer = new MyCountDownTimer(60000,1000);

        //设置Button点击事件触发倒计时
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myCountDownTimer.start();
            }
        });
    }

    private class MyCountDownTimer extends CountDownTimer {

        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        //计时过程
        @Override
        public void onTick(long l) {
            //防止计时过程中重复点击
            btn.setClickable(false);
            btn.setText(l/1000+"秒");

        }

        //计时完毕的方法
        @Override
        public void onFinish() {
            //重新给Button设置文字
            btn.setText("重新获取");
            //设置可点击
            btn.setClickable(true);
        }
    }

    public void apara_login(View v) {
        boolean result = loginVM.Apare_login(userName.getText().toString().trim(), password.getText().toString().trim());
        if(result) {
            sendImage.setBackgroundResource(R.drawable.sendok);
            sendText.setText("send is ok");
            Intent intent = new Intent(MainActivity.this,MainPageActivity.class);
            startActivity(intent) ;
        }
        else {
            sendImage.setBackgroundResource(R.drawable.sendfail);
            sendText.setText("send is fail");
        }

    }

    @Override
    public void onClick(View v) {
//        switch (v.getId()){
//            case R.id.bulb_cancel_btn:
//                mOutletDialog.hide();
//                break;
//            case R.id.start_btn:
//                Log.d(TAG, "onClick: ");
//                v1.setVisibility(View.INVISIBLE);
//                v2.setVisibility(View.VISIBLE);
//        }

    }
}


